package com.party.mobile.web.controller.logistics;

import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.model.logistics.WlParcelOutput;
import com.party.core.service.logistics.biz.LogisticsBizService;
import com.party.mobile.web.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 物流控制器
 * Created by yifeng
 *
 * @date 2018/4/25 0025
 * @time 下午 14:13
 */
@Controller
@RequestMapping("logistics/logistics")
public class LogisticsController {

    @Autowired
    private LogisticsBizService logisticsBizService;

    @Value("#{kuaidiniao['kuaidiniao.eBusinessID']}")
    private String eBusinessID;
    @Value("#{kuaidiniao['kuaidiniao.apiKey']}")
    private String apiKey;

    Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 根据业务id获取包裹信息
     *
     * @param targetId 业务id 商城的业务主订单id；礼包的领取id
     * @return
     */
    @ResponseBody
    @RequestMapping("findParcelByTargetId")
    public AjaxResult findParcelByTargetId(String targetId) {
        if (StringUtils.isEmpty(targetId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "业务id不能为空");
        }
        try {
            List<WlParcelOutput> parcelListOutputs = logisticsBizService.findParcelByTargetId(targetId);
            return AjaxResult.success(parcelListOutputs);
        } catch (Exception e) {
            logger.error("获取包裹列表异常", e);
            return AjaxResult.error(100, "获取包裹列表异常");
        }
    }

    /**
     * 根据包裹编号获取物流信息，及追踪详情
     *
     * @param parcelId 包裹编号
     * @return
     */
    @ResponseBody
    @RequestMapping("findLogisticsByParcelId")
    public AjaxResult findLogisticsByParcelId(String parcelId) {
        if (StringUtils.isEmpty(parcelId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "包裹编号不能为空");
        }
        try {
            Map<String, Object> resultMap = logisticsBizService.findLogisticsById(parcelId, apiKey, eBusinessID);
            return AjaxResult.success(resultMap);
        } catch (Exception e) {
            logger.error("获取物流信息异常", e);
            return AjaxResult.error(100, "获取物流信息异常");
        }
    }
}
